package com.hoe.tsb.fraft.client.role;

import com.hoe.tsb.Configuration;
import com.hoe.tsb.fraft.client.ClientRoleBridge;
import com.hoe.tsb.fraft.event.msg.Heartbeat;

/**
 * <p>
 * 领导者角色
 * </p>
 *
 * @author hoe
 * @version 1.0
 * @date 2022/12/5 16:23
 */
public class Leader extends AbstractRole {

    public Leader(ClientRoleBridge bridge, Configuration configuration, RoleObserver roleObserver) {
        super(bridge, configuration, roleObserver);
    }

    @Override
    protected void doTimeout() {
        //广播心跳
        mBridge.broadcast(new Heartbeat());
    }

    @Override
    public void onRoleTurn() {
        //任期加一
        int term = mRoleObserver.getTerm();
        mRoleObserver.setTerm(++term);
        //广播心跳
        mBridge.broadcast(new Heartbeat());
        //广播心跳后应立即重置超时时间，不然可能出现连续广播心跳
        resetTimeout();
    }

}
